#ifndef __ADVANCEDTIMER_H_
#define __ADVANCEDTIMER_H_

#include "stm32h7xx.h"
#include "stm32_types.h"
#include "stm32h7xx_hal.h"



// 在头文件中添加以下结构体和变量声明
typedef struct {
    float start_freq;        // 启动频率 (Hz)
    float max_freq;         // 最大频率 (Hz)
    float end_freq;         // 结束频率 (Hz)
    float acceleration;     // 加速度 (Hz/s)
    uint32_t total_pulses;  // 总脉冲数
    
    // 运行时状态
    uint32_t current_pulse; // 当前脉冲计数
    float current_freq;     // 当前频率
    uint32_t accel_pulses;  // 加速阶段脉冲数
    uint32_t decel_pulses;  // 减速阶段脉冲数
    uint32_t const_pulses;  // 匀速阶段脉冲数
    
    enum {
        ACCEL_PHASE,        // 加速阶段
        CONST_PHASE,        // 匀速阶段
        DECEL_PHASE,        // 减速阶段
        STOP_PHASE          // 停止阶段
    } phase;
    
    uint8_t is_running;     // 运行状态标志
    float duty_cycle;       // 占空比
} StepperControl_t;

extern uint32_t get_tim8_clock_frequency(void);
extern TIM_HandleTypeDef htim8;
extern void TIM8_Trapezoidal_Start(void);
extern void TIM8_Trapezoidal_Init(float start_freq, float max_freq, float end_freq, 
                          float acceleration, uint32_t total_pulses, float duty_cycle);

static void TIM8_Update_Frequency(float freq);

/* USER CODE BEGIN Private defines */
extern void Stepper_Trapezoidal_Control(float max_freq, float acceleration, unsigned int total_steps);
extern void TIM8_CH1_PWM_Control(uint32_t timer_freq, uint32_t pwm_freq, float duty_cycle, int32_t pulse_count);
//extern void TIM8_CH1_PWM_Control(unsigned int timer_freq, unsigned int pwm_freq, float duty_cycle, unsigned int pulse_count);
/* USER CODE END Private defines */

void MX_TIM8_Init(void);

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);




#endif

